//-------------------------------------------------------------------------
/**
   File:   Util.h

   Descr.: Preliminary utility file

   Author: Stefan Leihkauf
   Date:   23.03.2014
*/
//-------------------------------------------------------------------------
#ifndef _UTIL_H
#define _UTIL_H
#include "common/types/typedef.h"

//-------------------------------------------------------------------------
// some helper functions
//-------------------------------------------------------------------------

namespace cb { namespace common { namespace util {

//-------------------------------------------------------------------------
// split string
//-------------------------------------------------------------------------
StringList StringSplit(const StdString& str, const StdString& delim);

//-------------------------------------------------------------------------
// check if string is convertible to an integral number
//-------------------------------------------------------------------------
bool isIntegralNumber(const StdString& s);

//-------------------------------------------------------------------------
// Convert string to number (of given template type)
// return converted string as number or 0 if conversion failes
//-------------------------------------------------------------------------
template <typename T>
T StringToNumber(const StdString& str)
{
    StringStream ss(str);
    T result;
    return ((ss >> result) ? result : static_cast<T> (0));
}

//-------------------------------------------------------------------------
// trim from start
static inline StdString& StringTrimLeft(StdString& s)
{
    s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
    return s;
}

//-------------------------------------------------------------------------
// trim from end
static inline StdString& StringTrimRight(StdString& s)
{
    s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
    return s;
}

//-------------------------------------------------------------------------
// trim from both ends
static inline StdString& StringTrimLeftRight(StdString& s)
{
    return StringTrimLeft(StringTrimRight(s));
}

}}} // namespace cb::common::util

#endif //!defined _UTIL_H